package com.beiding.workordersystem.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beiding.workordersystem.system.dao.AreaDao;
import com.beiding.workordersystem.system.dao.CityDao;
import com.beiding.workordersystem.system.dao.ProvinceDao;
import com.beiding.workordersystem.system.entity.AreaEntity;
import com.beiding.workordersystem.system.entity.CityEntity;
import com.beiding.workordersystem.system.entity.ProvinceEntity;
import com.beiding.workordersystem.system.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    private ProvinceDao provinceDao;
    @Autowired
    private CityDao cityDao;
    @Autowired
    private AreaDao areaDao;

    @Override
    @Cacheable("region")
    public List<ProvinceEntity> listAllProvince() {

        List<ProvinceEntity> list = provinceDao.selectList(new QueryWrapper<>());

        for (ProvinceEntity provinceEntity : list) {
            List<CityEntity> cityEntities = cityDao.selectList(new QueryWrapper<CityEntity>().lambda().eq(CityEntity::getFather, provinceEntity.getProvinceId()));
            provinceEntity.setCities(cityEntities);
            for (CityEntity cityEntity : cityEntities) {
                List<AreaEntity> areaEntities = areaDao.selectList(new QueryWrapper<AreaEntity>().lambda().eq(AreaEntity::getFather, cityEntity.getCityId()));
                cityEntity.setAreas(areaEntities);
            }
        }


        return list;
    }
}
